package com.masterclient.app.businessmodule.minemodule.point;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebSettings;
import android.widget.ImageView;
import android.widget.TextView;

import com.eoner.managerlibrary.system.SystemConfigStorage;
import com.eoner.managerlibrary.sobot.SobotManager;
import com.masterclient.app.BaseActivity;
import com.masterclient.app.MainActivity;
import com.masterclient.app.R;
import com.masterclient.app.businessmodule.usermodule.login.index.QuickLoginActivity;
import com.masterclient.app.businessmodule.minemodule.point.model.PointDrawData;
import com.eoner.baselib.utils.status.StatusBarUtil;
import com.masterclient.app.widget.TitlebarView;
import com.sobot.chat.SobotApi;
import com.sobot.chat.api.enumtype.SobotChatTitleDisplayMode;
import com.sobot.chat.api.model.Information;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import wendu.dsbridge.DWebView;

public class PointScanResultActivity extends BaseActivity {

    @BindView(R.id.title_view)
    TitlebarView title_view;

    @BindView(R.id.success_layout)
    View success_layout;

    @BindView(R.id.point)
    TextView point;
    @BindView(R.id.result_desc)
    TextView result_desc;
    @BindView(R.id.query)
    TextView query;

    @BindView(R.id.result_icon)
    ImageView result_icon;

    @BindView(R.id.dwebview)
    DWebView dwebView;

    String code;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.point_scan_result);
        ButterKnife.bind(this);
        StatusBarUtil.setStatusBarColor(getActivity(), Color.parseColor("#FFFFFF"));
        init();
        addListener();
    }

    private void init() {

        WebSettings webSettings = dwebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setSupportZoom(false);
        webSettings.setBuiltInZoomControls(true);
        webSettings.setDisplayZoomControls(false);
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        webSettings.setAllowFileAccess(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setLoadsImagesAutomatically(true);
        webSettings.setDefaultTextEncodingName("utf-8");
        webSettings.setUserAgentString(webSettings.getUserAgentString() + " app:xrk");

        dwebView.loadUrl(SystemConfigStorage.getInstance().getIntegralExchangeUrl());

        PointDrawData data = (PointDrawData) getIntent().getSerializableExtra("data");
        code = getIntent().getStringExtra("code");
        if (data != null){
            showData(data);
        }else {
            loadFail(getIntent().getStringExtra("msg"));
        }


    }

    private void addListener() {

        title_view.setOnViewClick(new TitlebarView.onViewClick() {
            @Override
            public void leftClick() {
                finish();
            }

            @Override
            public void rightClick() {

            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

    }

    @OnClick({R.id.query})
    void onClick(View v) {
        switch (v.getId()) {
//            case R.id.home0:
//            case R.id.home1:
//                startActivitry(MainActivity.class,new String [][]{});
//                finish();
//                break;
            case R.id.service:
                //CustomerService.getInstance(mContext).connectService("金币服务",null, Config.CUSTOMER);
                if(isLogin()){
                    Information info = SobotManager.getBaseInfo();
                    //SobotApi.setChatTitleDisplayMode(mContext, SobotChatTitleDisplayMode.ShowFixedText,"金币服务");
                    SobotApi.setChatTitleDisplayMode(mContext, SobotChatTitleDisplayMode.Default, "");
                    SobotApi.startSobotChat(PointScanResultActivity.this, info);
                }else{
                    startActivity(new Intent(mContext, QuickLoginActivity.class));
                }
                break;
            case R.id.query:
                if("查看金币".equals(query.getText().toString().trim())){
                    startActivity(new Intent(mContext, PointActivity.class));
                }else{
                    startActivitry(MainActivity.class,new String [][]{});
                    finish();
                }
                break;
        }
    }





    public void showData(PointDrawData data) {

        //1 领取成功 2 商品已经被领取 3 领取失败 4 无效code
        if (data!=null&&!TextUtils.isEmpty(data.getSh_toast_title())){
            showToast(data.getSh_toast_title());
        }

        if(TextUtils.isEmpty(code)){
            point.setText(data.getSh_point());
            point.setTextColor(Color.parseColor("#C2C5CC"));
            result_desc.setText("很抱歉，请扫描正确的金币二维码信息！");
            result_desc.setTextColor(Color.parseColor("#E62E2E"));
            query.setText("返回首页");
            query.setBackgroundResource(R.drawable.point_scan_result_home_bkg);
            query.setTextColor(Color.parseColor("#141922"));
            success_layout.setBackgroundResource(R.mipmap.point_scan_result_fail_head_img_bkg);
            result_icon.setImageResource(R.mipmap.point_scan_result_fail_icon);
            return;
        }
        if("1".equals(data.getSh_status())){
            point.setText(data.getSh_point());
            point.setTextColor(Color.parseColor("#241E19"));
            result_desc.setText("金币领取成功");
            result_desc.setTextColor(Color.parseColor("#241E19"));
            query.setText("查看金币");
            query.setBackgroundResource(R.drawable.point_scan_result_query_bkg);
            query.setTextColor(Color.parseColor("#FFFFFF"));
            success_layout.setBackgroundResource(R.mipmap.point_scan_result_success_head_img_bkg);
            result_icon.setImageResource(R.mipmap.point_scan_result_success_icon);
        }else if("2".equals(data.getSh_status())){
            point.setText(data.getSh_point());
            point.setTextColor(Color.parseColor("#C2C5CC"));
            result_desc.setText("金币领取失败，因为金币已被领取过");
            result_desc.setTextColor(Color.parseColor("#E62E2E"));
            query.setText("返回首页");
            query.setBackgroundResource(R.drawable.point_scan_result_home_bkg);
            query.setTextColor(Color.parseColor("#141922"));
            success_layout.setBackgroundResource(R.mipmap.point_scan_result_fail_head_img_bkg);
            result_icon.setImageResource(R.mipmap.point_scan_result_fail_icon);
        }else if("4".equals(data.getSh_status())){
            point.setText("领取失败");
            point.setTextColor(Color.parseColor("#E62E2E"));
            point.setTextSize(19);
            point.setCompoundDrawablesWithIntrinsicBounds(mContext.getResources().getDrawable(R.mipmap.point_scan_result_fail_icon1),null,null,null);
            result_desc.setText("很抱歉，请扫描正确的金币二维码信息！");
            result_desc.setTextColor(Color.parseColor("#E62E2E"));
            query.setText("返回首页");
            query.setBackgroundResource(R.drawable.point_scan_result_home_bkg);
            query.setTextColor(Color.parseColor("#141922"));
            success_layout.setBackgroundResource(R.mipmap.point_scan_result_fail_head_img_bkg);
            result_icon.setVisibility(View.GONE);
        }else if("3".equals(data.getSh_status())){
            point.setText("领取失败");
            point.setTextColor(Color.parseColor("#E62E2E"));
            point.setTextSize(19);
            point.setCompoundDrawablesWithIntrinsicBounds(mContext.getResources().getDrawable(R.mipmap.point_scan_result_fail_icon1),null,null,null);
            result_desc.setText("当前金币暂不支持领取！");
            result_desc.setTextColor(Color.parseColor("#E62E2E"));
            query.setText("返回首页");
            query.setBackgroundResource(R.drawable.point_scan_result_home_bkg);
            query.setTextColor(Color.parseColor("#141922"));
            success_layout.setBackgroundResource(R.mipmap.point_scan_result_fail_head_img_bkg);
            result_icon.setVisibility(View.GONE);
        }else{
            loadFail("领取金币异常");
        }

    }


    public void loadFail(String s) {
        point.setText("扫描失败");
        point.setTextColor(Color.parseColor("#E62E2E"));
        point.setTextSize(19);
        point.setCompoundDrawablesWithIntrinsicBounds(mContext.getResources().getDrawable(R.mipmap.point_scan_result_fail_icon1),null,null,null);
        result_desc.setText("很抱歉，请扫描正确的二维码信息！");
        result_desc.setTextColor(Color.parseColor("#E62E2E"));
        query.setText("返回首页");
        query.setBackgroundResource(R.drawable.point_scan_result_home_bkg);
        query.setTextColor(Color.parseColor("#141922"));
        success_layout.setBackgroundResource(R.mipmap.point_scan_result_fail_head_img_bkg);
        result_icon.setVisibility(View.GONE);
    }

}
